package com.zhanghu.backendZray.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhanghu.backendZray.cache.ContextCache;
import com.zhanghu.backendZray.cache.UserCache;
import com.zhanghu.backendZray.cache.UserMenuCache;
import com.zhanghu.backendZray.constants.CityConstant;
import com.zhanghu.backendZray.converter.UserConverter;
import com.zhanghu.backendZray.mapper.UserMapper;
import com.zhanghu.backendZray.pojo.request.city.CitySettingRequest;
import com.zhanghu.backendZray.pojo.request.user.ChangePasswordRequest;
import com.zhanghu.backendZray.pojo.request.user.UserBaseInfoRequest;
import com.zhanghu.backendZray.pojo.request.user.UserRequest;
import com.zhanghu.backendZray.service.UserService;
import com.zhanghu.backendZray.pojo.entity.User;
import com.zhanghu.backendZray.utils.Md5Util;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Optional;

//@DS("slave_1")
@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final UserConverter userConverter;

    @Override
    public void add(UserRequest userRequest) {
        convertPasswordMd5(userRequest);
        User user = userConverter.input2Entity(userRequest);
        user.setId(null);
        baseMapper.insert(user);
        UserCache.putUser(user);
    }

    private static void convertPasswordMd5(UserRequest userRequest) {
        String password = Md5Util.get2Md5(userRequest.getPassword());
        userRequest.setPassword(password);
    }

    @Override
    public void update(UserRequest userRequest) {
        convertPasswordMd5(userRequest);
        User user = userConverter.input2Entity(userRequest);
        baseMapper.updateById(user);
        UserCache.putUser(user);
    }

    @Override
    public void del(Long id) {
        baseMapper.deleteById(id);
        UserCache.removeUser(id.intValue());
        UserMenuCache.removeUserAllMenu(id.intValue());
    }

    @Override
    public String getNameById(Integer id) {
        User user = baseMapper.selectOne(new LambdaQueryWrapper<>(User.class).eq(User::getId, id));
        return Optional.ofNullable(user).map(User::getName).orElse("");
    }

    @Override
    public void changePassword(ChangePasswordRequest request) {
        Assert.isTrue(request.getNewPassword().equals(request.getRepeatNewPassword()), "两次新密码输入不一致！");
        Integer id = request.getId();
        User user = getById(id);
        Assert.isTrue(user != null, "此用户不存在！");
        String oldPassword = request.getOldPassword();
        String calOldPassword = Md5Util.get2Md5(oldPassword);
        String password = user.getPassword();
        Assert.isTrue(password.equals(calOldPassword), "旧密码不正确！");
        String newPassword = Md5Util.get2Md5(request.getNewPassword());
        user.setPassword(newPassword);
        updateById(user);
    }

    @Override
    public void namePhoneSetting(UserBaseInfoRequest request) {
        Integer id = request.getId();
        User user = getById(id);
        Assert.isTrue(user != null, "此用户不存在！");
        String phone = user.getPhone();
        if (StrUtil.isEmpty(phone)) {
            Assert.isTrue(StrUtil.isNotEmpty(request.getPassword()), "初次设置请输入密码");
            user.setPassword(Md5Util.get2Md5(request.getPassword()));
        }
        user.setName(request.getName());
        user.setPhone(request.getPhone());
        updateById(user);
    }

    @Override
    public User getByWxNickName(String wxNickName) {
        User user = getOne(new LambdaQueryWrapper<User>().eq(User::getWxNickName, wxNickName));
        return user;
    }

    /**
     * 用户配置常驻城市
     */
    @Override
    public void citySetting(CitySettingRequest request) {
        String city = request.getCity();
        List<String> cityList = CityConstant.CITY_RESULT_LIST;
        Assert.isTrue(cityList.contains(city), "城市名不符合要求");
        Integer currentUserId = ContextCache.currentUserId();
        User user = getById(currentUserId);
        Assert.isTrue(user != null, "此用户不存在！");
        user.setCity(city);
        updateById(user);
    }

    /**
     * 获取用户常驻城市
     * @return
     */
    @Override
    public String getCitySetting() {
        User user = getById(ContextCache.currentUserId());
        return Optional.ofNullable(user).map(User::getCity).orElse(null);
    }

}